package com.dreamer.service.mobile.impl;

import com.dreamer.domain.mall.goods.Goods;
import com.dreamer.domain.mall.transfer.Transfer;
import com.dreamer.domain.pmall.goods.PmallGoods;
import com.dreamer.domain.user.*;
import com.dreamer.domain.user.enums.AccountsTransferStatus;
import com.dreamer.domain.user.enums.AccountsType;
import com.dreamer.domain.user.enums.AgentLevelEnum;
import com.dreamer.service.mobile.TransferHandler;
import com.dreamer.repository.mobile.AccountsDao;
import com.dreamer.service.mobile.*;
import com.dreamer.util.PreciseComputeUtil;
import com.dreamer.util.RewardUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ps.mx.otter.exception.ApplicationException;

import java.util.*;

/**
 * Created by huangfei on 02/07/2017.
 */
@Service
public class AccountsHandlerImpl extends BaseHandlerImpl<Accounts> implements AccountsHandler {


    /**
     * 减少账户
     * 生成记录
     *
     * @param accounts
     * @param amount
     */
    public AccountsRecord deductAccountAndRecord(AccountsType accountsType, Agent agent, Agent casedAgent, Double amount, String more) {
        Accounts accounts = agent.getAccounts();
        Double nowAmount = deductAccount(accountsType, accounts, amount);
        AccountsRecord record = new AccountsRecord(agent, casedAgent, accountsType, more, amount, nowAmount, new Date(), AccountsRecord.SUB);
        return record;
    }


    /**
     * 减少账户
     * 生成记录
     *
     * @param accounts
     * @param amount
     */
    public AccountsRecord deductAccountAndRecord(AccountsType accountsType, Agent agent, Agent casedAgent, Double amount, String more, Boolean validate) {
        Accounts accounts = agent.getAccounts();
        Double nowAmount = accounts.getAccount(accountsType);
        if (amount != 0) {//不为零的时候才通知
            nowAmount = deductAccount(accountsType, accounts, amount, validate);
        }
        AccountsRecord record = new AccountsRecord(agent, casedAgent, accountsType, more, amount, nowAmount, new Date(), AccountsRecord.SUB);
        if (amount == 0) {
            record.setInfo(record.getInfo().replace("追回", "通知"));
        }
        return record;
    }

    /**
     * 增加账户
     * 生成记录
     *
     * @param accounts
     * @param amount
     */
    public AccountsRecord increaseAccountAndRecord(AccountsType accountsType, Agent agent, Agent casedAgent, Double amount, String more) {
        Accounts accounts = agent.getAccounts();
        Double nowAmount = accounts.getAccount(accountsType);
        if (amount != 0) {//不为零的时候才通知
            nowAmount = increaseAccount(accountsType, accounts, amount);
        }
        AccountsRecord record = new AccountsRecord(agent, casedAgent, accountsType, more, amount, nowAmount, new Date(), AccountsRecord.ADD);
        if (amount == 0) {
            record.setInfo(record.getInfo().replace("利润", "通知"));
        }
        return record;
    }


    /**
     * 提高账户
     *
     * @param accounts
     * @param amount
     */
    private Double increaseAccount(AccountsType accountsType, Accounts accounts, Double amount) {
        validateAmount(amount);
        Double balance = accounts.getAccount(accountsType);//获取当前要转换的库存
        balance = PreciseComputeUtil.add(balance, amount);//加法计算
        accounts.setAccount(balance, accountsType);//设置为新的值
        return balance;
    }


    /**
     * 减少账户
     *
     * @param accounts
     * @param amount
     */
    private Double deductAccount(AccountsType accountsType, Accounts accounts, Double amount) {
//        validateAmount(amount);
//        Double balance = accounts.getAccount(accountsType);//获取当前要转换的库存
//        balance = PreciseComputeUtil.sub(balance, amount);//计算
//        validateResult(balance, accountsType);//验证结果
//        accounts.setAccount(balance, accountsType);//设置为新的值
        return deductAccount(accountsType, accounts, amount, true);
    }

    /**
     * 减少账户
     *
     * @param accounts
     * @param amount
     */
    private Double deductAccount(AccountsType accountsType, Accounts accounts, Double amount, boolean validate) {
        //四舍五入 保留两位小数
        validateAmount(amount);
        Double balance = accounts.getAccount(accountsType);//获取当前要转换的库存
        balance = PreciseComputeUtil.sub(balance, amount);//计算
        if (validate) {
            validateResult(balance, accountsType);//验证结果
        }
        accounts.setAccount(balance, accountsType);//设置为新的值
        return balance;
    }


    /**
     * 验证金额
     *
     * @param amount
     */
    private void validateAmount(Double amount) {
        if (amount <= 0 || !PreciseComputeUtil.isMoneyAmount(amount)) {//值非法
            throw new ApplicationException("金额输入错误！" + amount);
        }
    }


    /**
     * 验证结果
     *
     * @param result
     * @param accountsType
     * @return
     */
    private void validateResult(Double result, AccountsType accountsType) {
        if (result < 0) {
            throw new ApplicationException(accountsType.getStateInfo() + "余额不足");
        }
    }


    /**
     * 返回可用的代金券
     *
     * @param agent
     * @param amount
     * @return
     */
    public Double getAvailableVoucher(Agent agent, Double amount) {
        Double voucher = agent.getAccounts().getAccount(AccountsType.VOUCHER);
        if (voucher >= amount) {
            return amount;
        } else {
            throw new ApplicationException("代金券余额不足,需要" + amount);
        }
    }

    /**
     * 返回可用的进货券
     *
     * @param agent
     * @param amount
     * @return
     */
    public Double getAvailablePurchase(Agent agent, Double amount) {
        Double purchase = agent.getAccounts().getAccount(AccountsType.PURCHASE);
        amount = PreciseComputeUtil.round(amount * 0.03);//进货使用3%
        if (purchase >= amount) {
            return amount;
        } else {
            return purchase;//全用了
        }
    }


    @Override
    public Map<Agent, Double> rewardVoucher(List<Agent> parents, String voucherStr, Integer qunantity) {
        HashMap<Agent, Double> maps = new HashMap<>();
        //获得奖励参数
        Double[] vs = RewardUtil.getVsFromStr(voucherStr);
        //装载特殊获得奖励级别对应的获得奖励
        String[] tvipNames = new String[AgentLevelName.values().length - 1];
        for (int i = 0; i < AgentLevelName.values().length - 1; i++) {
            tvipNames[i] = AgentLevelName.values()[i].toString();
        }
        Map<String, Double> mapV = new HashMap<>();
        for (int i = 0; i < tvipNames.length; i++) {
            mapV.put(tvipNames[i], vs[i]);//特别vip的获得奖励
        }
        //截流总数
        Double sumReward = 0.0;
        //崔秀娟 特殊处理
        Boolean hasFenGs = false;
        //循环获得奖励
        for (int i = 0; i < parents.size(); i++) {
            Double result = 0.0;//该返的奖金
            String levelName = goodsAccountHandler.getMainGoodsAccount(parents.get(i)).getAgentLevel().getName();
            //如果是特殊vip
            if (mapV.containsKey(levelName)) {
                //增加获得奖励
                Double tem = PreciseComputeUtil.round(mapV.get(levelName) * qunantity);
                result = tem - sumReward;
                if (result < 0) {//减去已经返了的利润
                    result = 0.0;
                }
                sumReward += result;//累积已经返了的利
                //如果是静止或者苏雨薇
                if (parents.get(i).getAgentCode().equals("zmz934976") || parents.get(i).getAgentCode().equals("zmz015928")) {
                    //获取联盟单位与分公司的值的差价
                    Double cf = mapV.get(AgentLevelName.联盟单位.toString()) - mapV.get(AgentLevelName.分公司.toString());
                    result = result - cf * qunantity / 2;
                }
            }
            //加上基础的获得奖励
            result += RewardUtil.getVipVoucher(vs, i, qunantity);
            //如果获得奖励为0的大区不加入  其他级别也加入
            if (result.equals(0.0) && levelName.equals(AgentLevelName.大区.toString())) {
                continue;
            }
            //加入获得奖励map
            if (maps.containsKey(parents.get(i))) {
                Double temp = maps.get(parents.get(i));
                maps.put(parents.get(i), PreciseComputeUtil.add(temp, result));
            } else {
                maps.put(parents.get(i), result);
            }
        }
        return maps;
    }


    @Override
    public Map<Agent, Double> rewardPmallVoucher(List<Agent> parents, Integer qunantity, Double profit) {
        HashMap<Agent, Double> maps = new HashMap<>();
//        Map<String, Double> mapV = new HashMap<>();
        //截流总数
        Double sumReward = 0.0;
        //崔秀娟 特殊处理
        Boolean hasFenGs = false;
        //循环获得奖励
        for (int i = 0; i < parents.size(); i++) {
            Double result;//该返的奖金
            String levelName = agentHandler.getLevelName(parents.get(i));
            //增加获得奖励
            Double tem = getVipPerCent(parents.get(i)) * profit * qunantity;//获取额外的获得奖励
            result = tem - sumReward;
            if (result < 0) {//减去已经返了的利润
                result = 0.0;
            }
            sumReward += result;//累积已经返了的利
            //如果是崔秀娟 且有如果有分公司
            if (levelName.contains(AgentLevelName.分公司.toString())) {
                hasFenGs = true;
            }
            if (parents.get(i).getAgentCode() != null && parents.get(i).getAgentCode().equals("zmz126786") && hasFenGs) {
                result = result / 2;
            }

            if (i < basePercent.length) {
                //加上基础的获得奖励
                result += basePercent[i] * profit * qunantity;//获得奖励比*毛利润*数量
            }

            result = PreciseComputeUtil.round(result);

            //如果获得奖励为0不加入
            if (result.equals(0.0)) {
                continue;
            }
            //加入获得奖励map
            if (maps.containsKey(parents.get(i))) {
                Double temp = maps.get(parents.get(i));
                maps.put(parents.get(i), PreciseComputeUtil.add(temp, result));
            } else {
                maps.put(parents.get(i), result);
            }
        }
        return maps;
    }

    public Map<Agent, Double> rewardPmallVoucher(Agent agent, PmallGoods goods, Integer qunantity) {
        Map<Agent, Double> map = new HashedMap();
        Double[] vs = RewardUtil.getVsFromStr(goods.getVouchers());
        for (int i = 0; i < vs.length; i++) {
            Agent parent = agent.getParent();
            if(parent==null||parent.isMutedUser()){
                break;
            }
            Double tem = PreciseComputeUtil.mul(vs[i],qunantity);
            if(tem>0){
                map.put(parent,tem);
            }
            agent = agent.getParent();
        }
        return map;
    }


    /**
     * 将获得奖励模式解析出来
     * @param fromAgent 出货人  toAgent收货人
     * @return
     */

    public List<AccountsTransfer> rewardHYMVoucher(Agent fromAgent, Agent toAgent, Goods goods, Integer qunantity) {
        List<AccountsTransfer> list = new ArrayList<>();
        Map<String,Double[]> reward = RewardUtil.getRewardStr(goods.getVoucher());

        int jingliLevel = AgentLevelEnum.JINGLIHEHUOREN.getState();  //经理合伙人的等级
        int zongjianLevel = AgentLevelEnum.ZONGJIANHEHUOREN.getState();//总监合伙人的等级
        int zongcaiLevel = AgentLevelEnum.ZONGCAIHEHUOREN.getState(); //总裁合伙人等级
        int fengongsi = AgentLevelEnum.FENGONGSIHEHUOREN.getState(); //分公司合伙人等级
        int lianchuangLevel = AgentLevelEnum.LIANCHUANG.getState();  //联创等级
        AgentLevel toAgentLevel = agentHandler.getLevel(toAgent);  //收货人的AgentLevel
        AgentLevel fromAgentLevel = agentHandler.getLevel(fromAgent);  //发货人的AgentLevel

        //一、管理员给分公司合伙人转货要给分公司合伙人团长奖励
        if (fromAgent.isMutedUser()) {
            if(toAgentLevel.getLevel()==fengongsi){

                //团长奖
                AccountsTransfer teamHead = new AccountsTransfer();
                teamHead.setFromAgent(fromAgent);
                teamHead.setToAgent(toAgent);
                teamHead.setTransferTime(new Date());
                teamHead.setStatus(AccountsTransferStatus.TRANSFER);
                teamHead.setCanShow(true);
                teamHead.setRemark("团长奖励");
                Double tem = PreciseComputeUtil.mul(reward.get("teamHead")[0],qunantity);
                teamHead.setAmount(tem);
                list.add(teamHead);

                //分公司合伙人招募奖
                AccountsTransfer extra = new AccountsTransfer();
                extra.setFromAgent(fromAgent);
                extra.setToAgent(toAgent);
                extra.setTransferTime(new Date());
                extra.setStatus(AccountsTransferStatus.TRANSFER);
                extra.setCanShow(true);
                extra.setRemark("分公司合伙人招募奖励");
                Double tem1 = PreciseComputeUtil.mul(reward.get("extra")[3],qunantity);
                extra.setAmount(tem1);
                list.add(extra);
            }
        }
        //二、非管理员转货
        //先判断收货人
        // 1、合伙人有业绩奖和平推奖
        if(toAgentLevel.getLevel()>=fengongsi && toAgentLevel.getLevel()<=jingliLevel){
            //业绩奖
            AccountsTransfer achievement = new AccountsTransfer();
            achievement.setFromAgent(fromAgent);
            achievement.setToAgent(toAgent);
            achievement.setTransferTime(new Date());
            achievement.setStatus(AccountsTransferStatus.TRANSFER);
            achievement.setCanShow(true);
            achievement.setRemark("合伙人业绩奖励");
            if("经理合伙人".equals(toAgent.getLevelName())){
                Double tem = PreciseComputeUtil.mul(reward.get("achievement")[0],qunantity);
                achievement.setAmount(tem);
                list.add(achievement);
            }else if("总监合伙人".equals(toAgent.getLevelName())){
                Double tem = PreciseComputeUtil.mul(reward.get("achievement")[1],qunantity);
                achievement.setAmount(tem);
                list.add(achievement);
            }else if("总裁合伙人".equals(toAgent.getLevelName())){
                Double tem = PreciseComputeUtil.mul(reward.get("achievement")[2],qunantity);
                achievement.setAmount(tem);
                list.add(achievement);
            }else if("分公司合伙人".equals(toAgent.getLevelName())){
                Double tem = PreciseComputeUtil.mul(reward.get("achievement")[3],qunantity);
                achievement.setAmount(tem);
                list.add(achievement);
            }

            //平推奖
            Double equalityTem = PreciseComputeUtil.mul(reward.get("equality")[0],qunantity);
            AccountsTransfer equality = new AccountsTransfer();
            equality.setFromAgent(fromAgent);
            equality.setToAgent(toAgent);
            equality.setTransferTime(new Date());
            equality.setStatus(AccountsTransferStatus.TRANSFER);
            equality.setCanShow(true);
            equality.setAmount(equalityTem);
            equality.setRemark("平推奖励");
            list.add(equality);

            //招募奖,直接上级不是发货人
            Agent parent = toAgent.getParent();

            if(!fromAgent.getAgentCode().equals(parent.getAgentCode())){  //直接上级不是发货人

                AgentLevel parentLevel = agentHandler.getLevel(parent);  //收货人上级的AgentLevel

                AccountsTransfer extra = new AccountsTransfer();
                extra.setFromAgent(fromAgent);
                extra.setToAgent(parent);
                extra.setTransferTime(new Date());
                extra.setStatus(AccountsTransferStatus.TRANSFER);
                extra.setCanShow(true);
                extra.setRemark("合伙人招募奖励");

                if("经理合伙人".equals(parentLevel.getName())){
                    Double tem1 = PreciseComputeUtil.mul(reward.get("extra")[0],qunantity);
                    extra.setAmount(tem1);
                    list.add(extra);
                }

                if("总监合伙人".equals(parentLevel.getName())
                        && toAgentLevel.getLevel()>=zongcaiLevel && toAgentLevel.getLevel()<=zongjianLevel)
                {
                    Double tem1 = PreciseComputeUtil.mul(reward.get("extra")[1],qunantity);
                    extra.setAmount(tem1);
                    list.add(extra);
                }

                if("总裁合伙人".equals(parentLevel.getName()) && toAgentLevel.getLevel()==zongcaiLevel){
                    Double tem1 = PreciseComputeUtil.mul(reward.get("extra")[2],qunantity);
                    extra.setAmount(tem1);
                    list.add(extra);
                }

            }


        }

        // 2、联创的平推奖，合伙人给联创发货，联创的上级是联创
        if(fromAgentLevel.getLevel()>=zongcaiLevel && fromAgentLevel.getLevel()<=jingliLevel){
            Agent parent = toAgent.getParent();
            if(!fromAgent.getAgentCode().equals(parent.getAgentCode())) {  //直接上级不是发货人
                AgentLevel parentLevel = agentHandler.getLevel(parent);  //收货人上级的AgentLevel
                if(toAgentLevel.getLevel()==lianchuangLevel && parentLevel.getLevel()==lianchuangLevel){
                    //平推奖
                    Double equalityTem = PreciseComputeUtil.mul(reward.get("equality")[0],qunantity);
                    AccountsTransfer equality = new AccountsTransfer();
                    equality.setFromAgent(fromAgent);
                    equality.setToAgent(parent);
                    equality.setTransferTime(new Date());
                    equality.setStatus(AccountsTransferStatus.TRANSFER);
                    equality.setCanShow(true);
                    equality.setAmount(equalityTem);
                    equality.setRemark("平推奖励");
                    list.add(equality);
                }
            }

        }

        return list;
    }

    /**
     * 撤回奖励
     * @param fromAgent 出货人  toAgent收货人
     * @return
     */

    public List<AccountsTransfer> revokeHYMVoucher(Agent fromAgent, Agent toAgent, Goods goods, Integer qunantity) {
        List<AccountsTransfer> list = new ArrayList<>();
        Map<String,Double[]> reward = RewardUtil.getRewardStr(goods.getVoucher());

        int jingliLevel = AgentLevelEnum.JINGLIHEHUOREN.getState();  //经理合伙人的等级
        int zongjianLevel = AgentLevelEnum.ZONGJIANHEHUOREN.getState();//总监合伙人的等级
        int zongcaiLevel = AgentLevelEnum.ZONGCAIHEHUOREN.getState(); //总裁合伙人等级
        int fengongsi = AgentLevelEnum.FENGONGSIHEHUOREN.getState(); //分公司合伙人等级
        int lianchuangLevel = AgentLevelEnum.LIANCHUANG.getState();  //联创等级
        AgentLevel toAgentLevel = agentHandler.getLevel(toAgent);  //收货人的AgentLevel
        AgentLevel fromAgentLevel = agentHandler.getLevel(fromAgent);  //发货人的AgentLevel

        //一、管理员给分公司合伙人转货要给分公司合伙人团长奖励
        if (fromAgent.isMutedUser()) {
            if(toAgentLevel.getLevel()==fengongsi){

                //团长奖
                AccountsTransfer teamHead = new AccountsTransfer();
                teamHead.setFromAgent(toAgent);
                teamHead.setToAgent(fromAgent);
                teamHead.setTransferTime(new Date());
                teamHead.setStatus(AccountsTransferStatus.TRANSFER);
                teamHead.setCanShow(true);
                teamHead.setRemark("退回团长奖励");
                Double tem = PreciseComputeUtil.mul(reward.get("teamHead")[0],qunantity);
                teamHead.setAmount(tem);
                list.add(teamHead);

                //分公司合伙人招募奖
                AccountsTransfer extra = new AccountsTransfer();
                extra.setFromAgent(toAgent);
                extra.setToAgent(fromAgent);
                extra.setTransferTime(new Date());
                extra.setStatus(AccountsTransferStatus.TRANSFER);
                extra.setCanShow(true);
                extra.setRemark("退回分公司合伙人招募奖励");
                Double tem1 = PreciseComputeUtil.mul(reward.get("extra")[3],qunantity);
                extra.setAmount(tem1);
                list.add(extra);
            }
        }
        //二、非管理员转货
        //先判断收货人
        // 1、合伙人有业绩奖和平推奖
        if(toAgentLevel.getLevel()>=fengongsi && toAgentLevel.getLevel()<=jingliLevel){
            //业绩奖
            AccountsTransfer achievement = new AccountsTransfer();
            achievement.setFromAgent(toAgent);
            achievement.setToAgent(fromAgent);
            achievement.setTransferTime(new Date());
            achievement.setStatus(AccountsTransferStatus.TRANSFER);
            achievement.setCanShow(true);
            achievement.setRemark("退回合伙人业绩奖励");
            if("经理合伙人".equals(toAgent.getLevelName())){
                Double tem = PreciseComputeUtil.mul(reward.get("achievement")[0],qunantity);
                achievement.setAmount(tem);
                list.add(achievement);
            }else if("总监合伙人".equals(toAgent.getLevelName())){
                Double tem = PreciseComputeUtil.mul(reward.get("achievement")[1],qunantity);
                achievement.setAmount(tem);
                list.add(achievement);
            }else if("总裁合伙人".equals(toAgent.getLevelName())){
                Double tem = PreciseComputeUtil.mul(reward.get("achievement")[2],qunantity);
                achievement.setAmount(tem);
                list.add(achievement);
            }else if("分公司合伙人".equals(toAgent.getLevelName())){
                Double tem = PreciseComputeUtil.mul(reward.get("achievement")[3],qunantity);
                achievement.setAmount(tem);
                list.add(achievement);
            }

            //平推奖
            Double equalityTem = PreciseComputeUtil.mul(reward.get("equality")[0],qunantity);
            AccountsTransfer equality = new AccountsTransfer();
            equality.setFromAgent(toAgent);
            equality.setToAgent(fromAgent);
            equality.setTransferTime(new Date());
            equality.setStatus(AccountsTransferStatus.TRANSFER);
            equality.setCanShow(true);
            equality.setAmount(equalityTem);
            equality.setRemark("退回平推奖励");
            list.add(equality);

            //招募奖,直接上级不是发货人
            Agent parent = toAgent.getParent();

            if(!fromAgent.getAgentCode().equals(parent.getAgentCode())){  //直接上级不是发货人

                AgentLevel parentLevel = agentHandler.getLevel(parent);  //收货人上级的AgentLevel

                AccountsTransfer extra = new AccountsTransfer();
                extra.setFromAgent(parent);
                extra.setToAgent(fromAgent);
                extra.setTransferTime(new Date());
                extra.setStatus(AccountsTransferStatus.TRANSFER);
                extra.setCanShow(true);
                extra.setRemark("退回合伙人招募奖励");

                if("经理合伙人".equals(parentLevel.getName())){
                    Double tem1 = PreciseComputeUtil.mul(reward.get("extra")[0],qunantity);
                    extra.setAmount(tem1);
                    list.add(extra);
                }

                if("总监合伙人".equals(parentLevel.getName())
                        && toAgentLevel.getLevel()>=zongcaiLevel && toAgentLevel.getLevel()<=zongjianLevel)
                {
                    Double tem1 = PreciseComputeUtil.mul(reward.get("extra")[1],qunantity);
                    extra.setAmount(tem1);
                    list.add(extra);
                }

                if("总裁合伙人".equals(parentLevel.getName()) && toAgentLevel.getLevel()==zongcaiLevel){
                    Double tem1 = PreciseComputeUtil.mul(reward.get("extra")[2],qunantity);
                    extra.setAmount(tem1);
                    list.add(extra);
                }

            }


        }

        // 2、联创的平推奖，合伙人给联创发货，联创的上级是联创
        if(fromAgentLevel.getLevel()>=zongcaiLevel && fromAgentLevel.getLevel()<=jingliLevel){
            Agent parent = toAgent.getParent();
            if(!fromAgent.getAgentCode().equals(parent.getAgentCode())) {  //直接上级不是发货人
                AgentLevel parentLevel = agentHandler.getLevel(parent);  //收货人上级的AgentLevel
                if(toAgentLevel.getLevel()==lianchuangLevel && parentLevel.getLevel()==lianchuangLevel){
                    //平推奖
                    Double equalityTem = PreciseComputeUtil.mul(reward.get("equality")[0],qunantity);
                    AccountsTransfer equality = new AccountsTransfer();
                    equality.setFromAgent(parent);
                    equality.setToAgent(fromAgent);
                    equality.setTransferTime(new Date());
                    equality.setStatus(AccountsTransferStatus.TRANSFER);
                    equality.setCanShow(true);
                    equality.setAmount(equalityTem);
                    equality.setRemark("退回平推奖励");
                    list.add(equality);
                }
            }

        }

        return list;
    }


    private final String[] levelNames = {"VIP", "市代", "省代", "大区", "董事", "金董", "分公司", "联盟单位"};
//
    private final Integer[] paySum = {310, 2500, 4200, 16000, 32000, 64000, 128000, 1000000000};
//
    private final Double[] vipPercent = {0.05, 0.1, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6};
//
    private final Double[] basePercent = {0.3, 0.05, 0.05};


    //获取该拿多少百分比
    private Double getVipPerCent(Agent agent) {
        //暂时返回0
            return 0.0;
//        System.out.println(agent.getRealName()+"===");
//        Double payBalance = agent.getAccounts().getAccount(AccountsType.CONSUME);//消费金额
//        if (payBalance == null) payBalance = 0.0;
//        //如果不是游客
//        if (agent.getAgentCode() != null) {
//            String levelName = agentHandler.getLevel(agent).getName();
//            //不是亲情价
//            //如果是联盟单位
//            if (levelName.equals("联盟单位")) {
//                return vipPercent[vipPercent.length - 1];//返回最后一个百分比
//            }
//            //如果不是联盟单位 获取级别对用的消费
//            Integer index = getIndexFromArray(levelNames, levelName);
//            if (index < paySum.length && index > -1) {
//                payBalance += paySum[index];
//            }
//        }
//        //获取对应消费对应的获得奖励百分比
//        for (int i = 0; i < paySum.length; i++) {
//            if (payBalance < paySum[i]) {
//                Integer ii = i - 1;
//                if (ii < 0) {
//                    return 0.0;
//                } else {
//                    return vipPercent[ii];
//                }
//            }
//        }
//        return vipPercent[vipPercent.length - 1];
    }


    /**
     * 从数组中获取对应位置
     *
     * @param objects
     * @param o
     * @return
     */
    private Integer getIndexFromArray(Object[] objects, Object o) {
        for (int i = 0; i < objects.length; i++) {
            if (o.equals(objects[i])) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 从数组中获取对应区间
     *
     * @param objects
     * @param o
     * @return
     */
    private Integer getIndexFromArray(Integer[] objects, Object o) {
        for (int i = 0; i < objects.length; i++) {
            if (o.equals(objects[i])) {
                return i;
            }
        }
        return -1;
    }


    public static void main(String[] args) {
        System.out.println(2.2 / 2);
        final String[] levelNames = {"VIP", "市代", "省代", "大区", "董事", "金董", "分公司", "联盟单位"};

        final Integer[] paySum = {310, 2500, 4200, 16000, 32000, 64000, 128000, 1000000000};

        final Double[] percent = {0.05, 0.1, 0.15, 0.2, 0.3, 0.4, 0.5, 0.6};

        Double payBalance = 1222222.0;//消费金额
        if (payBalance == null) payBalance = 2200.0;
        //如果不是游客
        if (true) {
            String levelName = "金董";
            //不是亲情价
            //如果是联盟单位
            if (levelName.equals("联盟单位")) {
                System.out.println(percent[percent.length - 1]);
                return;
            }
            //如果不是联盟单位 获取级别对用的消费
            Integer index = new AccountsHandlerImpl().getIndexFromArray(levelNames, levelName);
            System.out.println(index);
            if (index < paySum.length && index > -1) {
                payBalance += paySum[index];
                System.out.println(payBalance);
            }
        }
        //获取对应消费对应的获得奖励百分比
        for (int i = 0; i < paySum.length; i++) {
            if (payBalance < paySum[i]) {
                Integer ii = i - 1;
                if (ii < 0) {
                    System.out.println(0.0);
                    return;
                } else {
                    System.out.println(ii);
                    System.out.println(percent[ii]);
                    return;
                }
            }
        }
        System.out.println(percent[percent.length - 1]);



    }

    private AccountsDao accountsDao;

    @Autowired
    private GoodsAccountHandler goodsAccountHandler;

    @Autowired
    private AgentHandler agentHandler;

    @Autowired
    private AgentLevelHandler agentLevelHandler;

    private AccountsRecordHandler accountsRecordHandler;
    public AccountsDao getAccountsDao() {
        return accountsDao;
    }

    @Autowired
    private TransferHandler transferHandler;

    @Autowired
    public void setAccountsDao(AccountsDao accountsDao) {
        this.accountsDao = accountsDao;
        super.setBaseDao(accountsDao);
    }
}
